package com.sc.test;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.sc.test.utils.ZxingUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Shen Chao on 2018-03-17-017 上午 8:40.
 */

public class ZxingActivity extends AppCompatActivity {
    @BindView(R.id.iv_zxing)
    ImageView ivZxing;
    @BindView(R.id.et_zxing_content)
    EditText etZxingContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zxing);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_scan, R.id.bt_shibie, R.id.bt_shengcheng})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_scan:
                new IntentIntegrator(this)
                        //设置扫码类型
//                PRODUCT_CODE_TYPES 商品码类型
//                ONE_D_CODE_TYPES 一维码类型
//                QR_CODE 二维码类型
//                ALL_CODE_TYPES 所有码类型
                        //不建议设置所有码，会附带扫描其他码
                        .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
                        //扫描提示信息
                        .setPrompt("请对准二维码")
                        //摄像头id
                        .setCameraId(0)
                        //扫完“哔”的一声
                        .setBeepEnabled(true)
                        //扫完后保存二维码图片，并把图片路径发给intent
                        .setBarcodeImageEnabled(false)
                        //设置超时时间，避免忘记关闭
//                        .setTimeout(5000)
                        //自定义扫描界面
                        .setCaptureActivity(ZxingScanActivity.class)
                        //设置方向不会被锁定
                        .setOrientationLocked(false)
                        //初始化
                        .initiateScan();
                break;
            case R.id.bt_shibie:
                break;
            case R.id.bt_shengcheng:
                String str = etZxingContent.getText().toString().trim();
                Bitmap bitmap = ZxingUtils.createBitmap(str);
                if (bitmap == null) {
                    Toast.makeText(this, "数据异常", Toast.LENGTH_SHORT).show();
                    return;
                }
                ivZxing.setImageBitmap(bitmap);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            if (intentResult.getContents() != null) {
                String result = intentResult.getContents();
                etZxingContent.setText(result);
                Toast.makeText(this, "扫描成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "扫描失败", Toast.LENGTH_SHORT).show();

            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
